require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'



class TC_Setup_test < Test::Unit::TestCase
  
    def startup
      
      $SiteName = 'Community Server'
      $SiteDescription = 'The platform that enables you to build rich, interactive communities.'
      $SiteUrl ='http://test.communityserver.com'
      $SerchMetaData = 'community server '
      $SearchMetaKeyWord ='community , interactive , social '
      $CopyRight = 'All rights reserved by Great Deccan '
      $Terms = 'Terms and Conditions'
      $Header= 'Additional Header'
      $Analytics = 'Analytics to identify'
      $AdminEmail='sajid@greatdeccan.com'
      $EmailEncoding = 'utf-8'
      $EmailSubjectEncoding = 'utf-8'
      $EmailThrottle = '-1'
      $SmtpServer = 'localhost'
      $SmtpUserName = 'sajid'
      $SmtpPassword = '123456'
      $SmtpPort = '25'
      $SecurityCode = 'Security Code'
      $EmailDomain = 'localhost'
      $Host = 'localhost'
      $Host_Edit ='localhost'
      $UserName ='sajid'
      $UserName_Edit = 'sajid1234'
      $Password = '123456'
      $Password_Edit ='123456'
      @@ie = Watir::IE.start($SITE_URL)
      end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
     
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
   def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
      
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifySystemAdminLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
      end
      
      
    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
        assert(ie.contains_text('Site Administration'))      
    end
    
    def test_09_verifyLinksExist
        # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Setup').exists?)
        assert(ie.link(:text, 'Manage Licenses').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
     
    def test_10_navigateToSiteSetup
        ie.goto($SITE_URL + 'controlpanel/Settings/Setup.aspx')
         
    end
     
    def test_11_navigateToBasicConfiguration
        assert(ie.text_field(:id, /SiteName$/).exists?)
        ie.text_field(:id, /SiteName$/).set($SiteName)
        sleep 3
        assert(ie.text_field(:id, /SiteDescription$/).exists?)
        ie.text_field(:id, /SiteDescription$/).set($SiteDescription)
        assert(ie.select_list(:id, /Timezone$/).exists?)
        ie.select_list(:id, /Timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.select_list(:id, /DateFormat$/).exists?)
        ie.select_list(:id, /DateFormat$/).select('Month-Day-Year (6-1-2003)')
        assert(ie.select_list(:id, /TimeFormat$/).exists?)
        ie.select_list(:id, /TimeFormat$/).select('24 hour single digit (15:08)')
    end
    
    def test_12_navigateToApplications
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(1))")
        assert(ie.radio(:id, /Forum_0$/).exists?)
        assert(ie.radio(:id, /Forum_1$/).exists?)
        ie.radio(:id, /Forum_1$/).set
        ie.radio(:id, /Forum_0$/).clear
        assert(ie.radio(:id, /Weblog_0$/).exists?)
        assert(ie.radio(:id, /Weblog_1$/).exists?)
        ie.radio(:id, /Weblog_1$/).set
        ie.radio(:id, /Weblog_0$/).clear
        assert(ie.radio(:id, /MediaGallery_0$/).exists?)
        assert(ie.radio(:id, /MediaGallery_1$/).exists?)
        ie.radio(:id, /MediaGallery_1$/).set
        ie.radio(:id, /MediaGallery_0$/).clear
        assert(ie.radio(:id, /Hub_0$/).exists?)
        assert(ie.radio(:id, /Hub_1$/).exists?)
        ie.radio(:id, /Hub_1$/).set
        ie.radio(:id, /Hub_0$/).clear
        assert(ie.radio(:id, /BlogRoller_0$/).exists?)
        assert(ie.radio(:id, /BlogRoller_1$/).exists?)
        ie.radio(:id, /BlogRoller_1$/).set
        ie.radio(:id, /BlogRoller_0$/).clear
    end
  
    def test_13_navigateToGeneralSiteSettings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(2))")
        assert(ie.text_field(:id, /SiteUrl$/).exists?)
        ie.text_field(:id, /SiteUrl$/).set($SiteUrl )
        assert(ie.text_field(:id, /SearchMetaDescription$/).exists?)
        ie.text_field(:id, /SearchMetaDescription$/).set($SerchMetaData )
        assert(ie.text_field(:id, /SearchMetaKeywords$/).exists?)
        ie.text_field(:id, /SearchMetaKeywords$/).set($SearchMetaKeyWord )
        assert(ie.text_field(:id, /Copyright$/).exists?)
        ie.text_field(:id, /Copyright$/).set($CopyRight)
        assert(ie.text_field(:id, /TermsOfServiceUrl$/).exists?)
        ie.text_field(:id, /TermsOfServiceUrl$/).set($Terms)
        assert(ie.radio(:id, /EnableSectionLocalization_0$/).exists?)
        assert(ie.radio(:id, /EnableSectionLocalization_1$/).exists?)
        ie.radio(:id, /EnableSectionLocalization_1$/).set
        ie.radio(:id, /EnableSectionLocalization_0$/).clear
        assert(ie.radio(:id, /EnableActivityLogging_0$/).exists?)
        assert(ie.radio(:id, /EnableActivityLogging_1$/).exists?)
        ie.radio(:id, /EnableActivityLogging_1$/).set
        ie.radio(:id, /EnableActivityLogging_0$/).clear
        sleep 2
        assert(ie.text_field(:id, /GenericHeader$/).exists?)
        sleep 2
        ie.text_field(:id, /GenericHeader$/).set($Header)
        sleep 2
        assert(ie.text_field(:id, /GoogleAnalytics$/).exists?)
        sleep 2
        ie.text_field(:id, /GoogleAnalytics$/).set($Analytics)
    end
    
   
  
    def test_14_navigateToEmailSettings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(3))")
        assert(ie.text_field(:id, /AdminEmailAddress$/).exists?)
        ie.text_field(:id, /AdminEmailAddress$/).set($AdminEmail)      
        assert(ie.radio(:id, /EnableEmail_0$/).exists?)
        assert(ie.radio(:id, /EnableEmail_1$/).exists?)
        ie.radio(:id, /EnableEmail_1$/).set
        ie.radio(:id, /EnableEmail_0$/).clear
        assert(ie.text_field(:id, /EmailEncoding$/).exists?)
        ie.text_field(:id, /EmailEncoding$/).set($EmailEncoding )
        assert(ie.text_field(:id, /EmailSubjectEncoding$/).exists?)
        ie.text_field(:id, /EmailSubjectEncoding$/).set($EmailSubjectEncoding)
        assert(ie.text_field(:id, /EmailThrottle$/).exists?)
        ie.text_field(:id, /EmailThrottle$/).set($EmailThrottle)
        assert(ie.text_field(:id, /SmtpServer$/).exists?)
        ie.text_field(:id, /SmtpServer$/).set($SmtpServer)
        assert(ie.radio(:id, /SmtpServerUsingSsl_0$/).exists?)
        assert(ie.radio(:id, /SmtpServerUsingSsl_1$/).exists?)
        ie.radio(:id, /SmtpServerUsingSsl_1$/).set
        ie.radio(:id, /SmtpServerUsingSsl_0$/).clear
        assert(ie.radio(:id, /SmtpServerUsingNtlm_0$/).exists?)
        assert(ie.radio(:id, /SmtpServerUsingNtlm_1$/).exists?)
        ie.radio(:id, /SmtpServerUsingNtlm_1$/).set
        ie.radio(:id, /SmtpServerUsingNtlm_0$/).clear
        assert(ie.radio(:id, /SmtpServerRequiredLogin_0$/).exists?)
        assert(ie.radio(:id, /SmtpServerRequiredLogin_1$/).exists?)
        ie.radio(:id, /SmtpServerRequiredLogin_1$/).set
        ie.radio(:id, /SmtpServerRequiredLogin_0$/).clear
        assert(ie.text_field(:id, /SmtpServerUserName$/).exists?)
        ie.text_field(:id, /SmtpServerUserName$/).set($SmtpUserName)
        assert(ie.text_field(:id, /SmtpServerPassword$/).exists?)
        ie.text_field(:id, /SmtpServerPassword$/).set($SmtpPassword)
        assert(ie.text_field(:id, /SmtpPortNumber$/).exists?)
        ie.text_field(:id, /SmtpPortNumber$/).set($SmtpPort)
        assert(ie.radio(:id, /EnableMailGateway_0$/).exists?)
        assert(ie.radio(:id, /EnableMailGateway_1$/).exists?)
        ie.radio(:id, /EnableMailGateway_0$/).set
        ie.radio(:id, /EnableMailGateway_1$/).clear
        assert(ie.text_field(:id, /MGSecurityCode$/).exists?)
        ie.text_field(:id, /MGSecurityCode$/).set($SecurityCode)
        assert(ie.text_field(:id, /MGEmailDomain$/).exists?)
        ie.text_field(:id, /MGEmailDomain$/).set($EmailDomain)
        assert(ie.link(:text , 'Add New Mailbox').exists?)
        ie.link(:text , 'Add New Mailbox').click
        sleep 5
        assert(ie.frame(:index, 1).text_field(:id, /AccountUrl$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountUrl$/).set($Host)
        assert(ie.frame(:index, 1).text_field(:id, /AccountUserName$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountUserName$/).set($UserName)
        assert(ie.frame(:index, 1).text_field(:id, /AccountPassword$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountPassword$/).set($Password)
        assert(ie.frame(:index, 1).select_list(:id, /AccountTypeList$/).exists?)
        ie.frame(:index, 1).select_list(:id, /AccountTypeList$/).set('IMAP')
        assert(ie.frame(:index, 1).checkbox(:id, /AccountEnabled$/).exists?)
        ie.frame(:index, 1).checkbox(:id, /AccountEnabled$/).set
        assert(ie.frame(:index, 1).link(:id, /SaveButton$/).exists?)
        ie.frame(:index, 1).link(:id, /SaveButton$/).click
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(3))")
        #Edit Mail Box
        sleep 2
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text == $Host)
              ok =1
               sleep 5
               table1[rowcount1][4].link(:text, 'Edit').click
               break
             end   
          rowcount1=rowcount1+1
        end
        sleep 5
        assert(ie.frame(:index, 1).text_field(:id, /AccountUrl$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountUrl$/).set($Host_Edit)
        assert(ie.frame(:index, 1).text_field(:id, /AccountUserName$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountUserName$/).set($UserName_Edit)
        assert(ie.frame(:index, 1).text_field(:id, /AccountPassword$/).exists?)
        ie.frame(:index, 1).text_field(:id, /AccountPassword$/).set($Password_Edit)
        assert(ie.frame(:index, 1).select_list(:id, /AccountTypeList$/).exists?)
        ie.frame(:index, 1).select_list(:id, /AccountTypeList$/).set('IMAP')
        assert(ie.frame(:index, 1).checkbox(:id, /AccountEnabled$/).exists?)
        ie.frame(:index, 1).checkbox(:id, /AccountEnabled$/).set
        assert(ie.frame(:index, 1).link(:id, /SaveButton$/).exists?)
        ie.frame(:index, 1).link(:id, /SaveButton$/).click
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(3))")
        # Delete Mail Box
        sleep 2
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text == $Host_Edit)
              ok =1
               sleep 5
               table1[rowcount1][4].link(:text, 'Delete').click
               break
             end   
          rowcount1=rowcount1+1
        end
        sleep 5
        assert(ie.frame(:index,1).button(:id,/OK_Button$/).exists?)
        ie.frame(:index,1).button(:id,/OK_Button$/).click
        sleep 3
    end
    
    def test_15_navigateToAggregateBehavior
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(4))")
        assert(ie.radio(:id, /WeblogPublicMode_0$/).exists?)
        assert(ie.radio(:id, /WeblogPublicMode_1$/).exists?)
        ie.radio(:id, /WeblogPublicMode_1$/).set
        ie.radio(:id, /WeblogPublicMode_0$/).clear
        assert(ie.radio(:id, /MediaGalleryPublicMode_0$/).exists?)
        assert(ie.radio(:id, /MediaGalleryPublicMode_1$/).exists?)
        ie.radio(:id, /MediaGalleryPublicMode_1$/).set
        ie.radio(:id, /MediaGalleryPublicMode_0$/).clear
    end
    
    def test_16_saveSetUp
        assert(ie.button(:id, /SaveButton$/).exists?)
        #ie.button(:id,/SaveButton$/).click
      end
      
    def test_99_closeIE
        ie.close
    end
    
end